<?php
namespace lemon\repository;

class Union
{
	
	/**
	 * 时间戳转为数字月份(于2020年1月份的月份差)
	 * @param unknown $time
	 * @return number
	 */
	public static function month2Number($time=null){
		if($time==null){
			$time = time();
		}
		$year = intval(date('Y', $time));
		$month = intval(date('m', $time));
		return ($year-2020)*12 + $month;
	}
	
	/**
	 * 数字月份(于2020年1月份的月份差)转为时间戳
	 * @param unknown $value
	 * @return number
	 */
	public static function number2Month($num){
		$month = (int)$num%12;
		if($month==0){
			$month=12;
			$num +=12;
		}
		$year = (int)$num/12 + 2020;
		return strtotime(intval($year).'-'.sprintf('%02s', $month).'-01');
	}
	
	public static function deExtension($url){
		$exts = ['jpg', 'png', 'gif'];
		$parsed = parse_url($url);
		$extension = "jpg";
		if(isset($parsed['host'])){
			switch ($parsed['host']){
				case "mmbiz.qpic.cn":
					if(isset($parsed['query'])){
						parse_str($parsed['query'], $params);
						$extension = $params['wx_fmt'];
					}
					break;
				default:
					$params = explode('.', $parsed['path']);
					if(count($params)==2){
						$extension = $params[1];
					}
					break;
			}
		}else{
			$params = explode('.', $parsed['path']);
			$len = count($params);
			if($len>1){
				$extension = $params[$len-1];
			}
		}
		if(!in_array($extension, $exts)){
			$extension = "jpg";
		}
		return $extension;
	}
}